<template>
  <div class="register-container">
    <div class="register-box">
      <el-form :model="registerForm" ref="formRef" label-width="80px" class="register-form" :rules="registerRules">
        <h2 class="form-title">用户注册</h2>
        <el-form-item label="姓名" prop="nickName">
          <el-input v-model="registerForm.nickName" autocomplete="off" ref="nickName" name="nickName"
            placeholder="请输入姓名" size="large" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" autocomplete="off" ref="username" name="username"
            placeholder="请输入用户名" size="large" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="userPwd">
          <el-input type="password" v-model="registerForm.userPwd" ref="userPwd" name="userPwd" autocomplete="off"
            placeholder="请输入密码" size="large" prefix-icon="Lock" show-password />
        </el-form-item>
        <!-- prop="confirmPassword" -->
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="registerForm.confirmPassword" autocomplete="off" ref="confirmPassword"
            name="confirmPassword" placeholder="请确认密码" size="large" prefix-icon="Lock" show-password />
        </el-form-item>
        <el-form-item class="button-group">
          <el-button type="primary" @click="register" size="large">注册</el-button>
          <el-button type="warning" @click="resetForm" size="large">重置</el-button>
          <el-button type="success" @click="goLogin" size="large">去登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
/**
 * 注册页面组件
 * 提供用户注册界面和功能
 */
import { ref } from "vue"
import { useRouter } from 'vue-router'
import { ElMessage, FormInstance } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue'
import { registerValidateApi, registerApi } from "../../api/index"
const router = useRouter()
/**
 * 注册表单数据模型
 */
const registerForm = ref({
  username: "",
  userPwd: "",
  confirmPassword: "",
  nickName: ''
})
const formRef = ref<FormInstance>()
/**
 * 验证用户名格式
 * @param rule - 验证规则
 * @param value - 用户名值
 * @param callback - 验证回调函数
 */
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    callback(new Error('用户名长度不能小于4位'))
  } else {
    callback()
  }
}
/**
 * 验证密码格式
 * @param rule - 验证规则
 * @param value - 密码值
 * @param callback - 验证回调函数
 */
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}
/**
 * 验证确认密码格式
 * @param rule - 验证规则
 * @param value - 确认密码值
 * @param callback - 验证回调函数
 */
const validateConfirmPassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}
/**
 * 验证姓名格式
 * @param rule - 验证规则
 * @param value - 姓名值
 * @param callback - 验证回调函数
 */
const validateNickName = (rule: any, value: any, callback: any) => {
  if (value.length >= 2 && value.length <= 6) {
    callback()
  } else {
    callback(new Error('姓名必须在2-6位'))
  }
}
/**
 * 注册表单验证规则集合
 */
const registerRules = {
  nickName: [{ required: true, trigger: 'blur', validator: validateNickName }],
  username: [{ required: true, validator: validateUsername }],
  userPwd: [{ required: true, trigger: 'blur', validator: validatePassword }],
  confirmPassword: [{ required: true, trigger: 'blur', validator: validateConfirmPassword }]
}

/**
 * 处理用户注册逻辑
 * 验证表单数据，校验用户名，提交注册信息
 */
const register = async () => {
  await formRef.value?.validate()
  if (registerForm.value.userPwd == registerForm.value.confirmPassword) {
    // 调用用户名校验接口
    await registerValidateApi(registerForm.value.username)
    //  整理参数
    const obj = {
      username: "",
      userPwd: "",
      nickName: ''
    }
    obj.username = registerForm.value.username
    obj.userPwd = registerForm.value.userPwd
    obj.nickName = registerForm.value.nickName
    //  调用注册接口
    await registerApi(obj)
    formRef.value?.resetFields()
    ElMessage.success("注册成功")
  } else {
    return ElMessage.error("密码和确定密码必须一致")
  }

}
/**
 * 跳转到登录页面
 */
const goLogin = () => {
  router.push({ path: "/login" });
}

/**
 * 重置注册表单
 */
const resetForm = () => {
  //重置表单
  formRef.value?.resetFields()
}

</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.register-box {
  width: 450px;
  padding: 40px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
}

.form-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 28px;
}

.button-group {
  display: flex;
  justify-content: space-between;
  margin-top: 30px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #555;
}
</style>